01 / 04

How do you cross-compile a Go binary for Linux from a macOS machine?

Set GOOS and GOARCH environment variables before go build. Go's toolchain includes full cross-compilation support with no separate toolchain needed for pure Go code.

Cross-compilation examples
Cross-compilation considerations
  1. 1

    CGO_ENABLED=0 produces a fully static binary — required for FROM scratch Docker images

  2. 2

    CGo complicates cross-compilation — use Docker buildx for services that require CGo

  3. 3

    GOARM=7 for 32-bit ARM (Raspberry Pi) alongside GOARCH=arm

  4. 4

    Version the binary with ldflags: -ldflags "-X main.version=$(git describe --tags)"

  5. 5

    Use goreleaser for automating multi-platform releases with changelogs and checksums